import { bind } from "../control/bind.js";
import {
  createScene,
  Toast,
  loadMarker,
  loadMeterial,
  loadMurderer,
  rotationY,
  rotationX,
} from "./components/loader.js";

export const name = "loadInSide";

export function Scene({ changeScene, camera, renderer }) {
  var camera_time = 0;

  const { scene, mesh } = createScene();

  loadMeterial(mesh, "inside");

  const onClick = (item) => {
    const name = item.name;
    if (name === "卧室") {
      Toast("小偷就在这里", 2000, scene, getPosition);
      loadMurderer(scene);
    } else if (name === "哈哈哈") {
      camera_time = 1;
    } else {
      Toast(`小偷不在${name}`, 2000, scene, getPosition);
    }
  };
  const interactPoints = [
    {
      name: "厨房",
      scale: 2,
      x: -30,
      y: 1,
      z: 20,
      onClick,
    },
    { name: "卧室", scale: 3, x: 48, y: 0, z: -20, onClick },
    { name: "出门", scale: 3, x: -5, y: 0, z: -20, onClick },
    { name: "哈哈哈", scale: 3, x: 1, y: 1, z: 25, onClick },
  ];

  const { interactMeshes, anchorMeshes } = loadMarker(interactPoints, {
    scene,
  });

  const {
    getPosition,
    setPosition,
    unbind,
    lookAt,
    cameraUpdate,
    fovJian,
    reset,
  } = bind({
    camera,
    renderer,
    interactMeshes,
  });

  function update() {
    rotationY(anchorMeshes);
    // rotationX(interactMeshes)
    if (camera_time === 50) {
      changeScene("loadOutSide");
    } else if (camera_time > 0) {
      fovJian();
      camera_time++;
    } else {
      cameraUpdate();
    }
  }

  function destroy() {
    unbind();
  }

  reset();
  return { scene, update, destroy };
}
